﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using infoMngmntGUI.Control;
using System.Globalization;

namespace infoMngmntGUI.View 
{
    /// <summary>
    /// Interaction logic for UserManagement_Main.xaml
    /// </summary>
    public partial class Add_Patient : IView
    {
        public Add_Patient() : base()
        {
            minimumAccess = Control.AccessManagement.UserType.NURSE;
            InitializeComponent();
            
            populateStateComboxBox();
        }

        /// <summary>
        /// Returns a string showing the purpose of this specific page - Registering a new patient
        /// </summary>
        /// <returns>The string, Register New Patient</returns>
        public override string ToString()
        {
            return "Register New Patient";
        }

        /*
         * Event handler for when the "Add Patient" button is clicked.  This button first validates all the fields on the page, 
         * and once confirming that all fields are valid, the handler extracts the data and passes it to DbConnection's AddPatient() 
         * static method to add the new patient data to the database.  
         */
        private void Add_Patient_Button_Press(object sender, RoutedEventArgs e)
        {
            if (validateInput())
            {
                string firstName = fNameTextBox.Text;
                string lastName = lNameTextBox.Text;
                string dob = (DateTime.Parse(DOB_field.Text)).ToString("yyyy-MM-dd");
                string street = streetTextBox.Text;
                string city = cityTextBox.Text;
                string state = stateComboBox.SelectedValue.ToString();
                string zip = zipTextBox.Text;
                string phone = phoneTextBox.Text;

                List<string> patientAttributes = new List<string>();
                patientAttributes.Add(firstName);
                patientAttributes.Add(lastName);
                patientAttributes.Add(dob);
                patientAttributes.Add(street);
                patientAttributes.Add(city);
                patientAttributes.Add(state);
                patientAttributes.Add(zip);
                patientAttributes.Add(phone);

                if (DbConnection.AddPatient(patientAttributes))
                {
                    string patientName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(firstName) + " " + CultureInfo.CurrentCulture.TextInfo.ToTitleCase(lastName);

                    MessageBox.Show("The patient, " + patientName + ", was successfully registered.",
                        "New Patient Registered", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);

                    fNameTextBox.Text = "";
                    lNameTextBox.Text = "";
                    DOB_field.Text = "";
                    streetTextBox.Text = "";
                    cityTextBox.Text = "";
                    stateComboBox.SelectedIndex = 0;
                    zipTextBox.Text = "";
                    phoneTextBox.Text = "";
                }
            }
        }

        /*
         * Uses regular expressions to validate the user input for each field entry.  If a particular field doesn't match 
         * to a given regex, then the user is notified via MessageBox that the data is invalid and needs to be adjusted.
         */
        private bool validateInput()
        {
            if (!Regex.IsMatch(fNameTextBox.Text, @"^[a-zA-Z]{3,10}$") || !Regex.IsMatch(lNameTextBox.Text, @"^[a-zA-Z]{3,10}$"))
            {
                MessageBox.Show("A name must contain only alphabetic characters and be between 3 and 10 letters in length.",
                    "Invalid name.", MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.OK);
                return false;
            }
            if (!Regex.IsMatch(streetTextBox.Text, @"^\d+\s[a-zA-Z]{3,}"))
            {
                MessageBox.Show("The street name must contain at least one digit, followed by a space, and at least three more letters.",
                    "Invalid street address", MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.OK);
                return false;
            }
            if (!Regex.IsMatch(cityTextBox.Text, @"^[^\d]{3,12}"))
            {
                MessageBox.Show("The city must contain only alphabetic characters and be between 3 and 12 letters in length.",
                    "Invalid city", MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.OK);
                return false;
            }
            if (!Regex.IsMatch(zipTextBox.Text, @"^\d{5}"))
            {
                MessageBox.Show("The zip code must be exactly 5 digits.",
                    "Invalid zip code", MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.OK);
                return false;
            }
            if (!Regex.IsMatch(phoneTextBox.Text, @"^\d{10}"))
            {
                MessageBox.Show("The phone number must be exactly 10 digits.",
                    "Invalid phone number", MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.OK);
                return false;
            }
            if (stateComboBox.SelectedIndex == 0)
            {
                MessageBox.Show("Please select a state from the drop down list.",
                    "Select a state", MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.OK);
                return false;
            }
            return true;
        }

        /*
         * Populates the stateComboBox control with a collection of all 50 states.
         */
        private void populateStateComboxBox()
        {
            List<string> states = new List<string>(50) {
                "Select State",
                "Alabama", 
                "Alaska",
                "Arizona",
                "Arkansas",
                "California",
                "Colorado",
                "Connecticut",
                "District of Columbia",
                "Delaware",
                "Florida",
                "Georgia",
                "Hawaii",
                "Idaho",
                "Illinois",
                "Indiana",
                "Iowa",
                "Kansas",
                "Kentucky",
                "Louisiana",
                "Maine",
                "Maryland",
                "Masschusetts",
                "Michigan",
                "Minnesota",
                "Mississippi",
                "Missouri",
                "Montana",
                "Nebraska",
                "Nevada",
                "New Hampshire",
                "New Jersey",
                "New Mexico",
                "New York",
                "North Carolina",
                "North Dakota",
                "Ohio",
                "Oklahoma",
                "Oregon",
                "Pennsylvania",
                "Rhode Island",
                "South Carolina",
                "South Dakota",
                "Tennessee",
                "Texas",
                "Utah",
                "Vermont",
                "Virginia",
                "Washington",
                "West Virginia",
                "Wisconsin",
                "Wyoming"
            };

            stateComboBox.ItemsSource = states;
            stateComboBox.SelectedIndex = 0;
        }

        /*
         * Removes watermark text when the field textbox control gains focus on the page
         */
        private void streetTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            streetTextBox.Text = "";
            streetTextBox.Foreground = Brushes.Black;
        }

        /*
         * Removes watermark text when the field textbox control gains focus on the page
         */
        private void cityTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            cityTextBox.Text = "";
            cityTextBox.Foreground = Brushes.Black;
        }

        /*
         * Removes watermark text when the field textbox control gains focus on the page
         */
        private void zipTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            zipTextBox.Text = "";
            zipTextBox.Foreground = Brushes.Black;
        }

        /*
         * Removes watermark text when the field textbox control gains focus on the page
         */
        private void phoneTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            phoneTextBox.Text = "";
            phoneTextBox.Foreground = Brushes.Black;
        }
    }
}
